package com.yenlien.traceyun.breed.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.breed.dao.BreedDao;
import com.yenlien.traceyun.breed.dao.BreedDetailDao;
import com.yenlien.traceyun.breed.dao.MotionTrailDao;
import com.yenlien.traceyun.breed.entity.Breed;
import com.yenlien.traceyun.breed.entity.BreedDetail;
import com.yenlien.traceyun.common.utils.Tools;
import com.yenlien.traceyun.webase.exception.BusinessException;

@Service
public class MotionTrailService {
    @Autowired
    private MotionTrailDao motionTrailDao;
    @Autowired
    private BreedDetailDao breedDetailDao;
    @Autowired
    private BreedDao breedDao;
    @Autowired
    private JdbcTemplate jdbc;

    public List<JSONObject> findAllCoordinate() {
        return motionTrailDao.findAllCoordinate();
    }

    public JSONObject findByEquipmentId(String equipmentId) {
        JSONObject json = new JSONObject();
        BreedDetail breedDetail = breedDetailDao.findByEquipmentId(equipmentId);
        if (breedDetail == null) {
            throw new BusinessException("A0001", "数据有误");
        }
        Breed breed = breedDao.findById(breedDetail.getRelevanceId());
        json.put("seedName", breed.getSeedName());
        json.put("batch", breed.getBatch());
        json.put("type", breedDetail.getType());
        json.put("code", breedDetail.getCode());
        json.put("equipmentName", breedDetail.getEquipmentName());
        return json;
    }

    public List<JSONObject> findAllBatch() {
        return breedDao.findAllBatch();
    }

    public List<JSONObject> findEquipment(String breedId) {
        return breedDetailDao.findEquipment(breedId);
    }

    public List<JSONObject> findMotionTrail(JSONObject params) {
        List<JSONObject> jsonList = new ArrayList<>();
        List<BreedDetail> breedDetailList = new ArrayList<>();
        String breedId = params.getString("breedId");
        String equipmentId = params.getString("equipmentId");
        String beginTime = "";
        String endTime = "";
        if (StringUtils.isEmpty(params.getString("beginTime"))) {
            beginTime = "1970-01-01 00:00:00";
        }
        if (StringUtils.isEmpty(params.getString("endTime"))) {
            endTime = Tools.getCurrentTime();
        }
        //如果设备ID为空，查询该批次下所有设备的轨迹
        if (StringUtils.isEmpty(equipmentId)) {
            breedDetailList = breedDetailDao.findByRelevanceId(breedId);
        } else {
            breedDetailList.add(breedDetailDao.findByEquipmentId(equipmentId));
        }
        //查询每个设备ID对应的轨迹
        for (BreedDetail breedDetail : breedDetailList) {
            if (StringUtils.isEmpty(params.getString("equipmentId"))) {
                equipmentId = breedDetail.getEquipmentId();
            }
            JSONObject json = new JSONObject();
            StringBuffer sql = new StringBuffer();
            sql.append("select coordinate from t_breed_motion_trail where equipment_id = '" + equipmentId + "'");
            sql.append(" and createtime between '" + beginTime + "' and '" + endTime + "'");
            SqlRowSet rowSet = jdbc.queryForRowSet(sql.toString());
            List<String> coordinateList = new ArrayList<>();
            while (rowSet.next()) {
                coordinateList.add(rowSet.getString("coordinate"));
            }
            json.put("equipmentName", breedDetail.getEquipmentName());
            json.put("coordinateList", coordinateList);
            jsonList.add(json);
        }
        return jsonList;
    }
}
